//
//  JHMainViewController.swift
//  JHSinaWeibo
//
//  Created by apple on 16/5/19.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class JHMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加了tabController的子视图控制器
       addChildViewControllers()
       let mainTabbar = JHMainTabbar()
        mainTabbar.plusBtn.addTarget(self, action:#selector(JHMainViewController.plusBtnClick)
            , forControlEvents: .TouchUpInside)
        
        print(tabBar.classForCoder)
        //只读属性不能够直接赋值，但是可以通过kvc间接赋值
        setValue(mainTabbar, forKey: "tabBar")
        print(tabBar.classForCoder)
    }

  @objc private func plusBtnClick() {
    print("点击了")
    }
   private func addChildViewControllers(){
    addChildViewController(JHTableViewController(),title: "首页",imageName: "tabbar_home")
    
    addChildViewController(JHMessageController(),title: "消息",imageName: "tabbar_message_center")
    
        addChildViewController(JHDiscoverController(),title: "发现",imageName: "tabbar_discover")
    
        addChildViewController(JHProfileController(),title: "我的",imageName: "tabbar_profile")
    }
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        
        //设置标签
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置vc导航视图控制器的导航条的模型对象的title
        //vc.navigationItem.title = title
        //如果导航条的title和vc的标签是一样的
        vc.title = title
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal:0 ,vertical:-3)
        //设置选中的图片 需要设置渲染模式
        
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置title的颜色
       //一次设置item的颜色   
        vc.tabBarItem .setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState:.Selected)
//  统一设置所有的item的颜色
//    tabBar.tintColor = UIColor.orangeColor()
        //内嵌到导航控制器中
        let nav = UINavigationController(rootViewController: vc)
        //添加子视图控制器
        addChildViewController(nav)
    }

}
